package com.whfc.ms.api.emp.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppPayrollAttachAddDTO;
import com.whfc.emp.dto.AppPayrollDTO;
import com.whfc.emp.dto.AppPayrollDetailDTO;
import com.whfc.emp.dto.AppPayrollEmpDTO;
import com.whfc.emp.param.AppPayrollAddParam;
import com.whfc.emp.param.AppPayrollDetailEditParam;
import com.whfc.emp.param.AppPayrollListParam;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsPayrollService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 人员工资
 *
 * @author guodong_sun
 * @Description:
 * @date 2019年9月3日
 */
@RequiresPermissions("emp/payroll")
@RestController
@RequestMapping("/ms/api/payroll")
public class AppPayrollController extends BaseController {

    @Autowired
    private MsPayrollService msPayrollService;

    /**
     * 工资发放记录列表
     *
     * @param user
     * @param request
     * @return
     */
    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated AppPayrollListParam request) {
        logger.info("工资发放记录列表，loginer:{},AppPayrollListParam:{}", user.getUsername(), request.toString());
        PageData<AppPayrollDTO> pageData = msPayrollService.list(request);
        return ResultUtil.success(pageData);
    }


    /**
     * 工资发放详情
     *
     * @param user
     * @param payrollId
     * @return
     */
    @GetMapping("/detail")
    public Result detail(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("payrollId") Integer payrollId) {
        logger.info("工资发放详情，loginer:{},payrollId:{}", user.getUsername(), payrollId);
        AppPayrollDetailDTO appPayrollDetailDTO = msPayrollService.detail(payrollId);
        return ResultUtil.success(appPayrollDetailDTO);
    }

    /**
     * 发放工资人员列表
     *
     * @param user
     * @param payrollId
     * @return
     */
    @GetMapping("/empPayrollList")
    public Result empPayrollList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @RequestParam("payrollId") Integer payrollId,
                                 @RequestParam(value = "pageSize") Integer pageSize,
                                 @RequestParam(value = "pageNum") Integer pageNum) {
        logger.info("发放工资人员列表，loginer:{},payrollId:{},pageSize：{}，pageNum：{}", user.getUsername(), payrollId, pageSize, pageNum);
        PageData<AppPayrollEmpDTO> data = msPayrollService.empPayrollList(payrollId, pageNum, pageSize);
        return ResultUtil.success(data);
    }


    /**
     * 删除工资记录
     *
     * @param user
     * @param payrollId
     * @return
     */
    @RequiresPermissions("emp/payroll/del")
    @PostMapping("/del/{payrollId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("payrollId") Integer payrollId) {
        logger.info("删除工资记录，loginer:{},payroll:{}", user.getUsername(), payrollId);
        msPayrollService.del(payrollId);
        return ResultUtil.success();
    }

    /**
     * 提交工资发放
     *
     * @param user
     * @param payrollId
     * @return
     */
    @RequiresPermissions("emp/payroll/commit")
    @PostMapping("/commit/{payrollId}")
    public Result commit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("payrollId") Integer payrollId) {
        logger.info("提交工资发放，loginer:{},payroll:{}", user.getUsername(), payrollId);
        msPayrollService.commit(payrollId);
        return ResultUtil.success();
    }

    /**
     * 添加工资发放附件
     *
     * @param user
     * @param file
     * @param payrollId
     * @return
     * @throws IOException
     */
    @RequiresPermissions("emp/payroll/upload")
    @PostMapping("/attach/add")
    public Result attachAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam("file") MultipartFile file,
                            @RequestParam("payrollId") Integer payrollId) throws IOException {
        logger.info("添加工资发放附件，loginer:{},payroll:{}", user.getUsername(), payrollId);
        AppPayrollAttachAddDTO appPayrollAttachAddDTO = msPayrollService.attachAdd(file.getOriginalFilename(), file.getInputStream(), payrollId);
        return ResultUtil.success(appPayrollAttachAddDTO);
    }


    /**
     * 删除工资附件
     *
     * @param user
     * @param attachId
     * @return
     */
    @RequiresPermissions("emp/payroll/del")
    @PostMapping("/attach/del/{attachId}")
    public Result attachDel(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("attachId") Integer attachId) {
        logger.info("删除工资附件，loginer:{},attachId:{}", user.getUsername(), attachId);
        msPayrollService.attachDel(attachId);
        return ResultUtil.success();
    }


    /**
     * 添加工资记录
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/payroll/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppPayrollAddParam request) {
        logger.info("添加工资记录，loginer:{},AppPayrollAddParam:{}", user.getUsername(), request.toString());
        msPayrollService.add(request);
        return ResultUtil.success();
    }

    /**
     * 修改员工工资信息
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/payroll/edit")
    @PostMapping("/detail/edit")
    public Result detailEdit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppPayrollDetailEditParam request) {
        logger.info("修改员工工资信息,loginer:{},AppPayrollDetailEditParam:{}", user.getUsername(), request.toString());
        msPayrollService.edit(request);
        return ResultUtil.success();
    }

    @PostMapping("/detail/export/{payrollId}")
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @PathVariable("payrollId") Integer payrollId) throws Exception {
        logger.info("导出工资详情,loginer:{},payrollId:{}", user.getUsername(), payrollId);
        OssPathDTO ossPathDTO = msPayrollService.export(payrollId);
        return ResultUtil.success(ossPathDTO);
    }

    @PostMapping("/detail/import")
    public Result importExcel(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("file") MultipartFile file,
                              @RequestParam("payrollId") Integer payrollId,
                              @RequestParam("clearingForm") Integer clearingForm) throws IOException {
        logger.info("导入工资模板,loginer:{},payrollId:{},clearingForm:{}", user.getUsername(), payrollId, clearingForm);
        msPayrollService.importExcel(file, payrollId, clearingForm);
        return ResultUtil.success();
    }

}
